<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Event
 *
 * @author tuancasi
 */
class Event extends CActiveRecord {

    const STATUS_PENDING = 0;
    const STATUS_APPROVE = 1;
    const STATUS_DENY = 2;
    const PAGE_SIZE = 4;

    public $userName;

    public static function model($className = __CLASS__) {
	return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
	return '{{cal_events}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
	// NOTE: you should only define rules for those attributes that
	// will receive user inputs.
	return array(
	    array('title, description, start', 'required', 'on' => 'insert'),
	    array('start, userName', 'safe')
	);
    }

    public function relations() {
	return array(
	    'user' => array(self::BELONGS_TO, 'User', 'user_id')
	);
    }

    public function attributeLabels() {
	return array(
	    'title' => 'Title',
	    'description' => 'Description',
	    'start' => 'Date'
	);
    }

    public function beforeSave() {
	if (parent::beforeSave()) {
	    if ($this->isNewRecord) {
		$this->created = $this->updated = date('Y-m-d', time());
		$this->user_id = user()->id;
	    } else {
		$this->updated = date('Y-m-d', time());
	    }
	    return true;
	}

	return false;
    }

    public function search() {
	$criteria = new CDbCriteria();
	$criteria->with = 'user';
	$criteria->compare('user.username', $this->userName, TRUE);
	$criteria->compare('t.title', $this->title, TRUE);
	$criteria->order = 't.id DESC';
	return new CActiveDataProvider($this, array(
	    'criteria' => $criteria,
	));
    }

}

